<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>初识Vue</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		
		<div id="root">
			<input type="text" placeholder="输入" v-model="keyWord"/>
			<ul>
				<li v-for="p in filPersons" :key="p.id">
                   {{p.name}}-{{p.age}}-{{p.sex}}
				</li>
			</ul>
			<button @click="sortType=2">升序</button>
			<button @click="sortType=1">降序</button>
			<button @click="sortType=0">原顺序</button>

		</div>

		<script type="text/javascript" >
			Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。

			//创建Vue实例
			new Vue({
				el:'#root',
				data:{
                  persons:[
				  {id:'001',name:'马冬梅',age:19,sex:'女'},
						{id:'002',name:'周冬雨',age:20,sex:'女'},
						{id:'003',name:'周杰伦',age:21,sex:'男'},
						{id:'004',name:'温兆伦',age:22,sex:'男'}
				  ],
				  keyWord:"",
				  sortType:0,//0是原数据1降序2升序
				},
				methods: {
					sheng(){

					},
					jiang(){

					},
					yuan(){

					}
				},
				computed: {
					filPersons(){
					    const arr	=this.persons.filter((p)=>{
                          return p.name.indexOf(this.keyWord) !== -1
					   })
					   //判断是否需要排序 //不等于0=true
					   if(this.sortType){
                         arr.sort((a,b)=>{
                            return this.sortType ==1? a.age-b.age : b.age-a.age //前面减后面是升序//b-a降序
						 })
					   }
					   return arr
					}
				}
				
			})

		</script>
	</body>
</html>
